package nl.ajonker.railroad.ui;

import nl.ajonker.railroad.pl.RailView;
import nl.ajonker.railroad.pl.TrackView;
import nl.ajonker.railroad.ui.drawcommand.DrawTrackCommand;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class JTrackPanel extends JPanel implements MouseListener, MouseMotionListener {
    private final int dimension;
    private final Point start;
    private final TrackView trackView;

    public JTrackPanel(Point start, TrackView trackView, int dimension) {
        super();
        this.start = start;
        this.trackView = trackView;
        this.dimension = dimension;
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    @Override
    public void paint(final Graphics g) {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        paintBackgroundImage();
        paintTrack(g2d);
        paintSwitches(g2d);
    }

    private void paintBackgroundImage() {
//        URL resource = this.getClass().getResource("/RockMossy0011_2_S.jpg");
//        Image image = new ImageIcon(resource).getImage();
//        g2d.drawImage(image, 0, 0, null);
        setBackground(new Color(240, 243, 129));
    }

    public void paintTrack(Graphics2D graphics2d) {
        RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        graphics2d.setRenderingHints(hints);
        new DrawTrackCommand().draw(start, graphics2d, dimension);
    }

    public void paintSwitches(Graphics2D graphics2d) {
        graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        for (RailView railView : trackView.getRailViews()) {
            railView.draw(graphics2d, dimension);
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        Graphics2D g2d = (Graphics2D) getGraphics();
        Point p = e.getPoint();
        for (RailView railView : trackView.getRailViews()) {
            Shape region = railView.getRegion(dimension);
            if (region.contains(p)) {
                trackView.switchRailPosition(railView.getName());
                railView.draw(g2d, dimension);
            }
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        Point p = e.getPoint();
        for (RailView railView : trackView.getRailViews()) {
            Shape region = railView.getRegion(dimension);
            if (region.contains(p)) {
                setCursor(new Cursor(Cursor.HAND_CURSOR));
                return;
            }
        }
        setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // do nothing

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // do nothing
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // do nothing
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // do nothing
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        // do nothing
    }
}
